package javafoundation.notes;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName Java读取CLOB
 * @Description Java读取Oracle数据库CLOB类型数据
 * @Author dzf
 * @Date 2022/8/2 14:32
 * @Version 1.0
 */
public class JavaReadCLOBType {

    public static void main(String[] args) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        for (int i = 1; i < 10; i++) {
            map1.put("CUN","第" + i +"次数据。");
            map1.put("NUT","第" + i +"次循环！");
            list.add(map1);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("MDF_RECORD" , JSON.toJSONString(list));
        String content = "";
        try {
            content = clobToString((Clob) map.get("MDF_RECORD"));
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
        List<Map> mapList = JSON.parseArray(content, Map.class);
    }


    /**
     *
     * @Description 将clob流转成string
     * @author dzf
     * @Date 2022/8/2 14:35
     * @Params [clob]
     * @return java.lang.String
     * @Version 1.0
     **/
    public static String clobToString(Clob clob) throws SQLException, IOException {
        String reString = "";
        Reader is = clob.getCharacterStream();//得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {
            sb.append(s);
            s = br.readLine();

        }
        reString = sb.toString();
        return reString;
    }
}
